﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace Business
{
    public static class CommentsManager
    {
        #region Create (Add)

        public static void Create(Comment comment)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                comment.Date = DateTime.Now;
                context.Comment.AddObject(comment);
                context.SaveChanges();
            }
        }

        #endregion

        #region Read (Get)

        public static List<Comment> Read()
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                return context.Comment.ToList();
            }
        }

        public static Comment Read(int id)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                try
                {
                    return context.Comment.Where(x => x.ID == id).First();
                }
                catch (Exception e)
                { return null; }
            }
        }

        #endregion

        #region Update (Set)

        public static void Update(Comment comment)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                Comment c = new Comment();
                c = context.Comment.Single(x => x.ID == comment.ID);
                c.Content = comment.Content;

                context.SaveChanges();
            }
        }

        #endregion

        #region Delete (Delete)

        public static void Delete(Comment comment)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                context.Comment.DeleteObject(comment);
                context.SaveChanges();
            }
        }

        public static void Delete(int ID)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                Comment c = context.Comment.Where(x => x.ID == ID).First(); ;
                context.Comment.DeleteObject(c);
                context.SaveChanges();
            }
        }

        #endregion
    }
}
